package com.zhipu.balance.mobile.dao;

import android.text.TextUtils;

import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.zhipu.balance.mobile.event.RecordCorn;
import com.zhipu.balance.mobile.event.RecordCorn_Table;
import com.zhipu.balance.mobile.event.RecordDaMi;
import com.zhipu.balance.mobile.event.RecordDaMi_Table;
import com.zhipu.balance.mobile.event.RecordDaMi;
import com.zhipu.balance.mobile.event.RecordDaMi_Table;

import java.util.List;

public class RecordDaMiDao {


    private RecordDaMiDao() {
    }

    private static final class SingletonHolder {
        private static final RecordDaMiDao INSTANCE = new RecordDaMiDao();
    }

    public static RecordDaMiDao getInstance() {
        return SingletonHolder.INSTANCE;
    }





    /**
     * 获取所有数据
     *
     * @return
     */
    public List<RecordDaMi> getAllList() {
        return SQLite.select().from(RecordDaMi.class).where().orderBy(RecordDaMi_Table.recordDaMiId, true).queryList();
    }



    /**
     * 分页获取
     *
     * @param nowPage
     * @param uniqueId
     * @return
     */
    public List<RecordDaMi> PagingFetch(int nowPage, String uniqueId) {
        if (TextUtils.isEmpty(uniqueId)) {
            return SQLite.select().from(RecordDaMi.class).where().orderBy(RecordDaMi_Table.recordDaMiId, false).limit(9).offset(nowPage).queryList();
        } else {
            return SQLite.select().from(RecordDaMi.class).where(RecordDaMi_Table.uniqueId.like("%" + uniqueId + "%")).orderBy(RecordDaMi_Table.recordDaMiId, false).limit(9).offset(nowPage).queryList();
        }
    }

    /**
     * 根据编号查询
     * @param productCategoryText
     * @return
     */
    public RecordDaMi QueryByNumber(String productCategoryText){
        RecordDaMi mRecordDaMi = SQLite.select().from(RecordDaMi.class).where(RecordDaMi_Table.uniqueId.eq(productCategoryText)).querySingle();
        if(null == mRecordDaMi){
            mRecordDaMi = new RecordDaMi();
        }
        return mRecordDaMi;
    }

    /**
     * 总条数
     * @return
     */
    public long TotalCount(String uniqueId){
        if(TextUtils.isEmpty(uniqueId)){
            return SQLite.selectCountOf().from(RecordDaMi.class).count();
        }else {
            return SQLite.selectCountOf().from(RecordDaMi.class).where(RecordDaMi_Table.uniqueId.eq(uniqueId)).count();
        }
    }

    /**
     * 根据编号获取数据
     * @param uniqueId
     * @return
     */
    public List<RecordDaMi> getUniqueIdList(String uniqueId){
        List<RecordDaMi> mRecordDaMiList = null;
        if (!TextUtils.isEmpty(uniqueId)) {
            mRecordDaMiList = SQLite.select().from(RecordDaMi.class).where(
                    RecordDaMi_Table.uniqueId.like("%" + uniqueId + "%"),
                    RecordDaMi_Table.impurityAverageValue.eq(false),
                    RecordDaMi_Table.imperfectGrainValue.eq(false),
                    RecordDaMi_Table.qualityOfBrokenValue.eq(false),
                    RecordDaMi_Table.yellowGrainRiceSampleValue.eq(false),
                    RecordDaMi_Table.massOfMixedSampleValue.eq(false)

            ).queryList();
        }
        return mRecordDaMiList;
    }
}
